﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using Clarius.Silverlight.Streaming.Library.Operations;

namespace Clarius.Silverlight.Streaming.Library
{
    public class AccountManager
    {
        Account account;

        int maxItems;
        List<FileSet> fileSets;

        public AccountManager(Account account)
            : this(account, Int32.MaxValue)
        {
        }
             
        public AccountManager(Account account, int maxItems)
        {
            this.account = account;
            this.maxItems = maxItems;
        }

        internal int MaxItems
        {
            get
            {
                return maxItems;
            }
        }

        public List<FileSet> FileSets
        {
            get
            {
                if (fileSets == null)
                {
                    RetrieveFileSetsOperation operation = new RetrieveFileSetsOperation(this);
                    fileSets = (List<FileSet>)operation.Execute();
                }

                return fileSets;
            }
        }

        /// <summary>
        /// Returns how much space (in bytes) is the account using
        /// </summary>
        /// <remarks>
        /// Silverlight Streaming Servers limit this to 4GB per account
        /// </remarks>
        public long Size
        {
            get
            {
                long accountSize = 0;

                foreach (FileSet fs in FileSets)
                {
                    accountSize += fs.Size;
                }

                return accountSize;
            }
        }

        /// <summary>
        /// Deletes a File Set from the server
        /// </summary>
        /// <param name="fileSetName">
        /// The name of the File Set to delete
        /// </param>
        public void DeleteFileSet(String fileSetName)
        {
            DeleteFileSetOperation operation = new DeleteFileSetOperation(this, fileSetName);
            operation.Execute();

            // try to keep the cached list of File Sets as much updated as possible
            FileSet fsToRemove = null;
            foreach (FileSet fs in fileSets)
            {
                if (fs.Name.ToLower() == fileSetName.ToLower())
                {
                    fsToRemove = fs;
                }
            }
            if (fsToRemove != null)
            {
                fileSets.Remove(fsToRemove);
            }
        }

        /// <summary>
        /// Uploads a File Set to the Silverlight Streaming Server
        /// </summary>
        /// <param name="fileSetName">
        /// The friendly name to assign to the uploaded File Set
        /// </param>
        /// <param name="applicationZipFile">
        /// The filename for the .zip file containing the File Set contents
        /// </param>
        /// <param name="overwrite">
        /// Determines if the specified File Set should be overwritten in case it already exists
        /// If this parameter is 'false' and a File Set with the same name already exists an exception
        /// will be thrown
        /// </param>
        public void UploadFileSet(String fileSetName, String applicationZipFile, bool overwrite)
        {
            UploadFileSetOperation operation = new UploadFileSetOperation(this, fileSetName, applicationZipFile, overwrite);
            operation.Execute();

            // try to keep the cached list of File Sets as updated as possible
            FileSets.Add(new FileSet(fileSetName, true, this));
        }

        /// <summary>
        /// Uploads a File Set to the Silverlight Streaming Server
        /// </summary>
        /// <param name="fileSetName">
        /// The friendly name to assign to the uploaded File Set
        /// </param>
        /// <param name="applicationZipFile">
        /// The filename for the .zip file containing the File Set contents
        /// </param>
        public void UploadFileSet(String fileSetName, String applicationZipFile)
        {
            UploadFileSet(fileSetName, applicationZipFile, false);
        }

        public FileSet GetFileSet (String fileSetName)
        {
            foreach (FileSet set in FileSets)
            {
                if (set.Name == fileSetName)
                {
                    return set;
                }
            }

            return null;
        }

        public Account Account
        {
            get { return account; }
        }
    }
}
